package com.yangchoi.jetpackcompose.ui.main.page

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.yangchoi.jetpackcompose.GlobalEvent
import com.yangchoi.jetpackcompose.GlobalViewModel
import com.yangchoi.jetpackcompose.LocalGlobalViewModel
import com.yangchoi.jetpackcompose.R
import com.yangchoi.jetpackcompose.ui.baseui.BaseComposePage
import com.yangchoi.jetpackcompose.ui.main.viewmodel.LoginEvent
import com.yangchoi.jetpackcompose.ui.main.viewmodel.LoginViewModel
import com.yangchoi.jetpackcompose.ui.widget.component.AutoSizingTextField
import com.yangchoi.jetpackcompose.ui.widget.component.CommonButton
import com.yangchoi.jetpackcompose.ui.widget.component.CommonTopBar
import com.yangchoi.jetpackcompose.ui.widget.ext.onClick

@Composable
fun LoginPage(navHostController: NavHostController,viewModel: LoginViewModel = hiltViewModel()){
    val globalCurrent = LocalGlobalViewModel.current

    var inputAccount by remember { mutableStateOf("") }
    var inputPassword by remember { mutableStateOf("") }

    var showPassword by remember { mutableStateOf(false) }

    var showLoading by remember { mutableStateOf(false) }

    BaseComposePage(
        showLoading = showLoading,
        topBarContent = {
            CommonTopBar(title = "登录", onBackClick = { navHostController.popBackStack() })
        }
    ) {
        Spacer(modifier = Modifier.height(100.dp))
        Row(modifier = Modifier
            .padding(horizontal = 20.dp)
            .height(48.dp)
            .border(width = 1.dp, color = Color.Black, shape = RoundedCornerShape(12.dp))) {
            Image(painter = painterResource(id = R.mipmap.icon_account), contentDescription = null, modifier = Modifier
                .padding(start = 12.dp)
                .size(25.dp)
                .align(Alignment.CenterVertically))
            Box(modifier = Modifier
                .padding(end = 12.dp, top = 3.dp, start = 5.dp)
                .fillMaxWidth()
                .align(Alignment.CenterVertically)){
                AutoSizingTextField(
                    value = inputAccount,
                    hintStr = "请输入账号",
                    singleLine = true,
                    onValueChange = {
                        inputAccount = it
                    })
            }
        }


        Spacer(modifier = Modifier.height(30.dp))
        Row(modifier = Modifier
            .padding(horizontal = 20.dp)
            .height(48.dp)
            .border(width = 1.dp, color = Color.Black, shape = RoundedCornerShape(12.dp))) {
            Image(painter = painterResource(id = if (showPassword) R.mipmap.icon_password_on else R.mipmap.icon_password_off), contentDescription = null, modifier = Modifier
                .padding(start = 12.dp)
                .size(25.dp)
                .align(Alignment.CenterVertically))
            Box(modifier = Modifier
                .padding(top = 3.dp, start = 5.dp)
                .weight(1f)
                .align(Alignment.CenterVertically)){
                AutoSizingTextField(
                    value = inputPassword,
                    hintStr = "请输入密码",
                    singleLine = true,
                    showPassword = showPassword,
                    onValueChange = {
                        inputPassword = it
                    })
            }
            Image(painter = painterResource(id = if (showPassword) R.mipmap.icon_eye_on else R.mipmap.icon_eye_off), contentDescription = null, modifier = Modifier
                .padding(horizontal = 12.dp)
                .size(25.dp)
                .align(Alignment.CenterVertically)
                .onClick {
                    showPassword = !showPassword
                },
                colorFilter = ColorFilter.tint(color = Color.Black)
            )
        }


        Spacer(modifier = Modifier.height(100.dp))
        CommonButton(btnTitle = "登 录", bgColor = Color.Black, titleStyle = TextStyle(fontSize = 16.sp, color = Color.White)) {
            if (inputAccount.isNotEmpty() && inputPassword.isNotEmpty()){
                showLoading = true
                viewModel.dispatch(event = LoginEvent.OnLogin,inputAccount,inputPassword, loginCallback = {
                    showLoading = false
                    globalCurrent.dispatch(event = GlobalEvent.OnSetLoginState, isLogin = true)
                    navHostController.popBackStack()
                })
            }
        }
    }
}